﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QueryMMaterialByMatRequirement : BasePage
    {
        int m_count = 0;
        public string m_strMainKey = "MaterialID";
        public DataTable MaterialRequirementDetailDT
        {
            get
            {
                if (Session["MaterialRequirementDetailDT"] == null)
                {
                    return CreateDT();
                }
                else
                {
                    return (DataTable)Session["MaterialRequirementDetailDT"];
                }
            }
            set
            {
                Session["MaterialRequirementDetailDT"] = value;
            }
        }
        public DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaterialRequirementDetailID");
            dt.Columns.Add("PlanDetailID");
            dt.Columns.Add("IsDirect");
            dt.Columns.Add("MaterialID");
            dt.Columns.Add("MaterialName");
            dt.Columns.Add("Specifications");
            dt.Columns.Add("Unit");
            dt.Columns.Add("MMNumber");//单件毫米数
            dt.Columns.Add("MeterWeight");//每米重量
            dt.Columns.Add("TotalNum");//总需求量
            dt.Columns.Add("StockNum");//库存数量
            dt.Columns.Add("OnWayNum");//在途数量
            dt.Columns.Add("MinOrderWeight");//最小送货量
            dt.Columns.Add("AdviseNum");//建议请购数量（毫米数）
            dt.Columns.Add("PieceNum");//件数
            dt.Columns.Add("Weight");//重量
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hdPlanID.Value = UIHelper.GetQueryStringValue("planid");
                hdMaterialRequirementID.Value = UIHelper.GetQueryStringValue("MaterialRequirementID");
                BindData(0);
            }
        }


        private void BindData(int pageIndex)
        {
            DataTable dt = BsMaterialRequirementBusiness.SelectDialogMaterial(hdPlanID.Value.Trim(), hdMaterialRequirementID.Value.Trim(), txtMaterialID.Text.Trim(), txtMaterialName.Text.Trim(), pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            dt.Columns.Add("IsDirectName");
            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    decimal AdviseNum = CommonMethod.ConvertToDecimal(dr["AdviseNum"]);
                    decimal MinOrderWeight = CommonMethod.ConvertToDecimal(dr["MinOrderWeight"]);
                    if (AdviseNum < MinOrderWeight) 
                    {
                        dr["AdviseNum"] = MinOrderWeight;
                    }
                }
                grdResult.DataSource = dt;
                grdResult.DataBind();
                DataView dv = new DataView(MaterialRequirementDetailDT);
                dv.RowFilter = "IsDel<>'1'";
                DataTable dtTemp = dv.ToTable();

                EnableGridViewRow(grdResult, dtTemp, m_strMainKey, false);
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        private void AddPro()
        {
            MaterialRequirementDetailDT = GetAddPro(MaterialRequirementDetailDT).Copy();
        }

        private DataTable GetAddPro(DataTable dt)
        {
            DataTable dtTemp = dt.Copy();
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    Label lblPlanDetailID = gvr.FindControl("lblPlanDetailID") as Label;
                    Label lblIsDirect = gvr.FindControl("lblIsDirect") as Label;
                    Label lblMaterialID = gvr.FindControl("lblMaterialID") as Label;
                    Label lblMaterialName = gvr.FindControl("lblMaterialName") as Label;
                    Label lblSpecifications = gvr.FindControl("lblSpecifications") as Label;
                    Label lblUnit = gvr.FindControl("lblUnit") as Label;
                    Label lblMMNumber = gvr.FindControl("lblMMNumber") as Label;
                    Label lblMeterWeight = gvr.FindControl("lblMeterWeight") as Label;
                    Label lblMinOrderWeight = gvr.FindControl("lblMinOrderWeight") as Label;
                    Label lblTotalNum = gvr.FindControl("lblTotalNum") as Label;
                    Label lblStockNum = gvr.FindControl("lblStockNum") as Label;
                    Label lblOnWayNum = gvr.FindControl("lblOnWayNum") as Label;
                    Label lblAdviseNum = gvr.FindControl("lblAdviseNum") as Label;

                    if (((CheckBox)gvr.FindControl("cbChoose")).Checked && gvr.Enabled)
                    {
                        DataRow[] drs = dtTemp.Select("PlanDetailID='" + lblPlanDetailID.Text.Trim() + "'  ");
                        if (drs.Length == 0)
                        {
                            DataRow dr = dtTemp.NewRow();
                            dr["MaterialRequirementDetailID"] = "-1";
                            dr["PlanDetailID"] = lblPlanDetailID.Text.Trim();
                            dr["IsDirect"] = lblIsDirect.Text.Trim();
                            dr["MaterialID"] = lblMaterialID.Text.Trim();
                            dr["MaterialName"] = lblMaterialName.Text.Trim();
                            dr["Specifications"] = lblSpecifications.Text.Trim();
                            dr["Unit"] = lblUnit.Text.Trim();
                            dr["MMNumber"] = lblMMNumber.Text.Trim();
                            dr["MeterWeight"] = lblMeterWeight.Text.Trim();
                            dr["MinOrderWeight"] = lblMinOrderWeight.Text.Trim();
                            dr["TotalNum"] = lblTotalNum.Text.Trim();
                            dr["AdviseNum"] = lblAdviseNum.Text.Trim();
                            dr["StockNum"] = lblStockNum.Text.Trim();
                            dr["OnWayNum"] = lblOnWayNum.Text.Trim();
                            dr["IsDel"] = "-1";
                            dr["IsNew"] = "1";
                            dtTemp.Rows.Add(dr);
                        }
                        else if (drs.Length > 0 && CommonMethod.GetString(drs[0]["IsDel"]) == "1")
                        {
                            drs[0]["PlanDetailID"] = lblPlanDetailID.Text.Trim();
                            drs[0]["IsDirect"] = lblIsDirect.Text.Trim();
                            drs[0]["MaterialID"] = lblMaterialID.Text.Trim();
                            drs[0]["MaterialName"] = lblMaterialName.Text.Trim();
                            drs[0]["Specifications"] = lblSpecifications.Text.Trim();
                            drs[0]["Unit"] = lblUnit.Text.Trim();
                            drs[0]["MMNumber"] = lblMMNumber.Text.Trim();
                            drs[0]["MeterWeight"] = lblMeterWeight.Text.Trim();
                            drs[0]["MinOrderWeight"] = lblMinOrderWeight.Text.Trim();
                            drs[0]["TotalNum"] = lblTotalNum.Text.Trim();
                            drs[0]["StockNum"] = lblStockNum.Text.Trim();
                            drs[0]["OnWayNum"] = lblOnWayNum.Text.Trim();
                            drs[0]["IsDel"] = "-1";
                        }
                    }
                    else
                    {
                        DataRow[] drs = dtTemp.Select("PlanDetailID='" + lblPlanDetailID.Text.Trim() + "'  ");
                        if (drs.Length > 0)
                        {
                            drs[0]["IsDel"] = "1";
                        }
                    }
                }
            }
            return dtTemp;
        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            AddPro();
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'success');CloseTopDialog();</script>");
        }

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            AddPro();
            BindData(e.PageIndex);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AddPro();
            BindData(0);
        }
    }
}